package services
{
	import definitions.AlbumDefinition;
	import definitions.ArtistDefinition;
	
	import model.ContentModel;
	
	import mx.collections.ArrayCollection;
	import mx.controls.List;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	public class ServiceMain 
	{
		public var isXmlMode:Boolean=true;

		private var artistsService:HTTPService;
		private var albumService:HTTPService;
		private var albumArtistsConnectionsService:HTTPService;
		public var artistsData:ArrayCollection;
		public var albumsData:ArrayCollection;
		public var albumsArtistsConnectionsData:ArrayCollection;
		public var albumsArtistsConnection:ArrayCollection;
		private var contentModel:ContentModel = ContentModel.getInstance();

		public function ServiceMain()
		{
			artistsService=new HTTPService();
			albumService=new HTTPService();
			albumArtistsConnectionsService=new HTTPService();
			artistsService.addEventListener(ResultEvent.RESULT, resultHandlerArtists);
			albumService.addEventListener(ResultEvent.RESULT, resultHandlerAlbums);
			albumArtistsConnectionsService.addEventListener(ResultEvent.RESULT, resultHandlerArtistsAlbumsConnections);
			if(isXmlMode){
				artistsService.resultFormat=albumService.resultFormat= albumArtistsConnectionsService.resultFormat='e4x';
				artistsService.url='xmldata/artists.xml';
				albumService.url='xmldata/albums.xml';
				albumArtistsConnectionsService.url='xmldata/albums-artists-connection.xml';
			}
		}
		
		public function loadArtists():void{
			artistsService.send();
			albumService.send();
			albumArtistsConnectionsService.send();
		}
		
		public function getArtists():ArrayCollection {			
			return artistsData;
		}

		private function resultHandlerArtists(event:ResultEvent):void
		{
			if(!isXmlMode){
				return;
			}
			artistsData=new ArrayCollection();
			var xArtists:XML=event.result as XML;
			var xEOF:Boolean=false;
			var xReturn:XML;
			var list:List=new List();
			var counter:int = 0;

			while (!xEOF)
			{

				try
				{
					xReturn=xArtists.artist[counter];

				}

				catch (err:Error)
				{
					xEOF=true;
				}
				
				if(!xReturn){
					xEOF=true;
				}

				if (!xEOF)
				{
					var artistDefinition:ArtistDefinition = new ArtistDefinition();
					artistDefinition.activeTillDateId = parseInt(xReturn.activeTillDateId);
					artistDefinition.activeFromDateId = parseInt(xReturn.activeFromDateId);
					artistDefinition.birthDateId = parseInt(xReturn.birthDateId);
					artistDefinition.deathDateId = parseInt(xReturn.deathDateId);
					artistDefinition.name = xReturn.name;
					artistDefinition.id = parseInt(xReturn.id);
					artistsData.addItem(artistDefinition);
					counter++;
					
				}
			}

			artistsData.refresh();
			contentModel.artistsData = artistsData;
			
		}
		
		private function resultHandlerAlbums(event:ResultEvent):void
		{
			if(!isXmlMode){
				return;
			}
			albumsData=new ArrayCollection();
			var xAlbums:XML=event.result as XML;
			var xEOF:Boolean=false;
			var xReturn:XML;
			var list:List=new List();
			var counter:int = 0;

			while (!xEOF)
			{

				try
				{
					xReturn=xAlbums.album[counter];

				}

				catch (err:Error)
				{
					xEOF=true;
				}
				
				if(!xReturn){
					xEOF=true;
				}

				if (!xEOF)
				{
					var albumDefinition:AlbumDefinition = new AlbumDefinition();
					albumDefinition.id = parseInt(xReturn.id);
					albumDefinition.releaseDateId = parseInt(xReturn.releaseDateId);
					albumDefinition.name = xReturn.name;
					albumsData.addItem(albumDefinition);
					counter++;					
				}
			}

			albumsData.refresh();
			contentModel.albumsData = albumsData;
			
		}
		
		private function resultHandlerArtistsAlbumsConnections(event:ResultEvent):void
		{
			if(!isXmlMode){
				return;
			}
			albumsArtistsConnectionsData=new ArrayCollection();
			var xConnections:XML=event.result as XML;
			var xEOF:Boolean=false;
			var xReturn:XML;
			var list:List=new List();
			var counter:int = 0;

			while (!xEOF)
			{

				try
				{
					xReturn=xConnections.connection[counter];

				}

				catch (err:Error)
				{
					xEOF=true;
				}
				
				if(!xReturn){
					xEOF=true;
				}

				if (!xEOF)
				{
					var connection:Object = new Object();
					connection.id = parseInt(xReturn.id);
					connection.albumId = parseInt(xReturn.albumId);
					connection.artistId = parseInt(xReturn.artistId);
					connection.connectionType = xReturn.connectionType;
					albumsArtistsConnectionsData.addItem(connection);
					counter++;					
				}
			}

			albumsArtistsConnectionsData.refresh();
			contentModel.albumsArtistsConnectionsData = albumsArtistsConnectionsData;
			
		}
	}
}
